

//阻止属性页面
/*function checkLeave(){
 event.returnValue="qwer";
 }*/
/*window.onbeforeunload=function(e){
 return "我在这写点东西";
 }*/
window.onunload=function(){
    alert("离开");
}
window.onload=function(){
    alert("加载完成");
}

/*//考试提醒
 function popup_warn(){

 }*/








//试卷加载
function loadPaper(){

    $.ajax({
        type:"post",
        url:"/stu/loadPaper.do",
        data:{
            id:$("#paperid").val()
        },
        dataType:"json",
        success:function (data) {
            //试卷所有属性
            aop.paper = data.paper;
            //考试时间存放在cookie中
            if (window.name == ""){
                window.name=data.paper.dura*60*1000;
            }
            //单选题库
            aop.onlyList = data.onlyList;
            //多选题库
            aop.manyList = data.manyList;
            //判断题库
            aop.judgeList = data.judgeList;
            //类型分值
            aop.types.only = getTypeScore("单选题",data.paper.struc);
            aop.types.many.a = getTypeScore("多选题",data.paper.struc);
            aop.types.many.b = Math.floor(getTypeScore("多选题",data.paper.struc)/2);
            aop.types.judge = getTypeScore("判断题",data.paper.struc);

        }
    });
}


//提交试卷
function getScore() {
    //setCookie("countDown",-1);

    if (completed()){
        submit_paper();
    }else {
        alert("未全部做完！");
    }

}
//提交试卷
function submit_paper() {
    var map = {
        onlyList:aop.onlyList,
        manyList:aop.manyList,
        judgeList:aop.judgeList
    };
    var webPaper ={
        sid:1,
        pid:aop.paper.paper_id,
        problems:map
    };
    $.ajax({
        type:"post",
        url:"/stu/getScore.do",
        data: JSON.stringify(webPaper),
        contentType: "application/json",
        success:function (data) {
            console.log(data.score);
        }
    });
}
/*检验是否做完*/
function completed() {
    //单选按钮选中的个数
    var onlys = $(":radio:checked");
    //复选按钮选中的个数
    var manys = 0;
    for (var i = 0;i<aop.manyList.length;i++){
        var name = "plm_many_"+(i+1);
        var jq = "input:checkbox[name='"+name+"']:checked";
        if ($(jq).length >0){
            manys++;
        }
    }
    //总个数
    var all = aop.manyList.length+aop.onlyList.length+aop.judgeList.length;
    //已选个数
    var abc = onlys.length+manys;
    if (abc == all){
        return true;
    }else {
        return false;
    }
}


/*试卷加载*/
var aop = new Vue({
    el:"#aop",
    data:{
        paper:{},
        onlyList:[],
        manyList:[],
        judgeList:[],
        types:{
            only:0,
            many:{
                a:0,
                b:0
            },
            judge:0,
        },
        submit:true
    },
    methods:{
        //针对单选题和判断题
        getOnlyAnswer:function (index,key) {
            this.onlyList[index].studentAnswer[0] = key;
        },
        getJudgeAnswer:function (index,key) {
            this.judgeList[index].studentAnswer[0] = key;
        }
    }
});


//定义倒计时的全局变量
var countDown;
//倒计时功能方法
function show_time(){
    //从cookie中获取倒计时间
    countDown = window.name;
    //计算时分秒
    if(countDown >=0){
        var hh = Math.floor(countDown / 3600000);
        var mm = Math.floor((countDown % 3600000) / 60000);
        var ss = Math.floor(((countDown % 3600000) % 60000)/1000 );
        if(countDown == 5*60*1000){
            layer.msg("距离考试还有五分钟",{icon:6});
        }
        if(countDown == 0){
            submit_paper();
        }
        countDown = countDown - 1000;

        window.name=countDown;

        if(hh < 10){
            hh = "0"+ hh;
        }
        if(mm <10){
            mm = "0"+mm;
        }
        if(ss <10){
            ss = "0"+ss;
        }
        $("#time").html(hh+":"+mm+":"+ss);
    }else {
        clearTime();
    }
}

//关闭计时器
function clearTime() {
    clearInterval(mytime);
    alert("考试时间结束");
}



//考试提醒
function startHint() {
    $(".main").css("display","none");
    layer.confirm("考试期间不允许对网页进行刷新、后退、关闭等操作，否则后果自负。如果在规定时间内没有交卷，系统将自动交卷。",
        {
            title: '考试规则',
            btn: ['请确认考试'],
            cancel:function () {
                return false;
            }
        }, function () {

            //执行试卷加载
            loadPaper();
            //执行倒计时
            var mytime = setInterval("show_time()", 1000);
            $(".main").css("display","block");
            layer.msg("考试开始", {icon: 1});
        });
}
startHint();





//根据类型获取当前试卷的分值
function getTypeScore(tname,arr) {
    var types = arr.split(",");
    for (var i = 0;i<types.length;i++){
        var sc = types[i].split("-");
        if(sc[0] == tname){
            return sc[1];
        }
    }
    return "";
}

//setCookie
function setCookie(name,value,expires){
    //expires 表示过期时间
    var date = new Date();
    date.setDate(date.getDate()+expires);
    document.cookie = name +"="+value+";expires="+expires;
}

//getCookie
function getCookie(key){
    var str = document.cookie.split("; ");
    for (var i = 0;i<str.length;i++){
        var arr = str[i].split("=");
        if(arr[0]==key){
            return arr[1];
        }
    }
    return "";
}
//removeCookie
function removeCookie() {
    var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
    if(keys) {
        for(var i = keys.length; i--;)
            document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
    }
}

